////preamble
capture log close //closes log file if open and always return 0 (captures error code)
clear all
//set mem 512m
set more off
display _newline(200)
set varabbrev off
set output proc

cd "C:\Users\fabio\OneDrive - FUCAPE Business School\Projetos\Emerson - Empreendedorismo ES_PT\data"
log using "empreend.log", replace 

use gem_2009_2015_pt_es_filtered_vars
renvars, lower

merge m:1 yrsurv country using "macro_controls.dta"
drop if _merge != 3
drop _merge

local lng en

ren yrsurv ano
ren gemhhinc renda
//ren gemeduc ensino
ren uneduc ensino
ren gender sexo
ren age idade_anos
ren opport perc_opo
ren suskill com_pre
ren fearfail int_ris

recode renda (33 = 0) (3467 = 1) (68100 = 2) (99999 = -2)
recode sexo (1=0) (2=1)
recode teayywhy (1=0) (2=1) (3=2)

if "`lng'" == "pt" {
	label define gem_yesno -2 "Recusou" -1 "Não sabe" 0 "Não" 1 "Sim"
	label var sexo "Sexo"
	label var ano "Ano"
	label var renda "Renda"
	label var idade_anos "Idade"
	label define gem_inc -2 "Impossível codificar" 0 "Tercil inf." 1 "Tercil intermed." 2 "Tercil sup." 99999 "Impossível codificar"
	label var ensino "Escolaridade"
	label var perc_opo "Percep. de Oportunidades" //Qi2.  In the next six months, will there be good opportunities for starting a business in the area where you live?
	label define oport 0 "Não vê oportunidades" 1 "Vê oportunidades"
	label var com_pre "Competência Prévia" //Qi3.  Do you have the knowledge, skill and experience required to start a new business?
	label define compet 0 "Não tem compet. prévia" 1 "Tem compet. prévia"
	label var int_ris "Intoler. ao Risco" //Qi4.  Would fear of failure would prevent you from starting a business?
	label define risco 0 "Medo não impede" 1 "Medo impede"
	label var equalinc "X4a: Prefer standard of living?"
	label var nbgoodc "X4b: Most people consider starting a new business a desirable career choice (PSa)"
	label var nbstatus "X4c: Those successful at starting a new business have a high level of status and respectc (PSb)"
	label var nbmedia "X4d: You will often see stories in the public media about successful new businesses (PSc)."
	label var teayywhy "Motiv. p/ Empreender" //Involved in Total early-stage Entrepreneurial Activity
	label var ecdec "X6a: I can decide on my own how I go about doing my work (WSa)"
	label var ecmean "X6b: The work I do is meaningful to me (WSb)"
	label var ecstr "X6c: At my work, I am not exposed to excessive stress (WSc)"
	label var ecsatf "X6d: I am satisfied with my current work (WSd)"
	label var ecinc "X6e: I am satisfied with my current income from work (WSe)"
	label define gem_likert -2 "Refused" -1 "Don't know" 1 "Strongly disagree" 2 "Somewhat disagree" 3 "Neither agree nor disagree" 4 "Somewhat agree" 5 "Strongly agree"

	label define edlevel -2 "Faltante" 0 "Pré-primário" 1 "1o estágio Ens. Bás." 2 "2o estágio Ens. Bás." 3 "Ensino Médio" 4 "Pós-Médio não Sup." 5 "1o estágio Ens. Sup." 6 "2o estágio Ens. Sup."
	label values ensino edlevel
	
	label define lsexo 0 "Masculino" 1 "Feminino"
	label values sexo lsexo
	
	label define lmotiv 0 "Totalm. Oport." 1 "Parcialm. Oport." 2 "Totalm. Necess."
	label values teayywhy lmotiv
}
else {
	label define gem_yesno -2 "Refused" -1 "Don't know" 0 "No" 1 "Yes"
	label var sexo "Gender"
	label define lsexo 0 "Male" 1 "Female"
	label values sexo lsexo
	//label values sexo female
	label var ano "Year"
	label var renda "Income"
	label var idade_anos "Age"
	label define gem_inc -2 "Cannot code" 0 "Low tercile income" 1 "Mid tercile income" 2 "Top tercile income" 99999 "Cannot code"
	label var ensino "Schooling"
	label var perc_opo "Perception of Opport." //Qi2.  In the next six months, will there be good opportunities for starting a business in the area where you live?
	label define oport 0 "Doesn't see opport." 1 "Sees opportunities"
	label var com_pre "Prev. Know. Skills" //Qi3.  Do you have the knowledge, skill and experience required to start a new business?
	label define compet 0 "Doesn't have prev. know/skills" 1 "Has previous know/skills"
	label var int_ris "Risk Intolerance" //Qi4.  Would fear of failure would prevent you from starting a business?
	label define risco 0 "Fear doesn't prevent" 1 "Fear prevents"
	label var equalinc "X4a: Prefer standard of living?"
	label var nbgoodc "X4b: Most people consider starting a new business a desirable career choice (PSa)"
	label var nbstatus "X4c: Those successful at starting a new business have a high level of status and respectc (PSb)"
	label var nbmedia "X4d: You will often see stories in the public media about successful new businesses (PSc)."
	label var teayywhy "Reason to Enterprise" //Involved in Total early-stage Entrepreneurial Activity
	//label values teayywhy gem_scale_motiv
	label var ecdec "X6a: I can decide on my own how I go about doing my work (WSa)"
	label var ecmean "X6b: The work I do is meaningful to me (WSb)"
	label var ecstr "X6c: At my work, I am not exposed to excessive stress (WSc)"
	label var ecsatf "X6d: I am satisfied with my current work (WSd)"
	label var ecinc "X6e: I am satisfied with my current income from work (WSe)"
	label define gem_likert -2 "Refused" -1 "Don't know" 1 "Strongly disagree" 2 "Somewhat disagree" 3 "Neither agree nor disagree" 4 "Somewhat agree" 5 "Strongly agree"
}

label values renda gem_inc

gen lingua = 0
replace lingua = 1 if country == 244 | country == 55 | country == 351 | country == 620
if "`lng'" == "pt" {
	label var lingua "Cultura"
}
else {
	label var lingua "Culture"
}

label define lang 0 "ES" 1 "PT"
label values lingua lang

local clist "Paises de lingua espanhola: "
levelsof country if lingua == 0, local(paises)

local lbe: value label country
foreach l of local paises {
	local c`l' : label `lbe' `l'
	local clist "`clist' `l' `c`l''; "
}
di "`clist'"

local clist "Paises de lingua portuguesa: "
levelsof country if lingua == 1, local(paises)

local lbe: value label country
foreach l of local paises {
	local c`l' : label `lbe' `l'
	local clist "`clist' `l' `c`l''; "
}
di "`clist'"

label define cont 0 "Others" 1 "South America"
gen continent = 0
replace continent = 1 if (country == 55 | country == 51 | country == 54 | country == 56 | country == 57 | country == 582 | country == 591 | country == 593 | country == 598)
label values continent cont
count if missing(continent)

label define colonized 0 "Colonized" 1 "Colonized"
gen colonized = 0
replace colonized = 1 if (country != 34 & country != 351)
label values colonized colonized

keep if renda >= 0

keep if ensino >= 0
//drop if ensino == 9999 //could not code

keep if sexo >= 0

keep if idade_anos >= 0

keep if perc_opo >= 0
keep if com_pre >= 0
keep if int_ris >= 0
keep if equalinc >= 0
label values perc_opo oport
label values com_pre compet
label values int_ris risco
label values equalinc gem_yesno

keep if nbgoodc >= 0
keep if nbstatus >= 0
keep if nbmedia >= 0
label values nbgoodc nbstatus nbmedia gem_yesno

egen perc_soc = rmean(equalinc nbgoodc nbstatus nbmedia)
if "`lng'" == "pt" {
	label var perc_soc "Percep. da Sociedade"
}
else {
	label var perc_soc "Perception of Society"
}

keep if teayywhy >= 0

label values ecdec ecmean ecstr ecsatf ecinc gem_likert

egen satisf = rmean(ecdec ecmean ecstr ecsatf ecinc)
if "`lng'" == "pt" {
	label var satisf "Satisfação c/ Trabalho"
}
else {
	label var satisf "Work satisfaction"
}

est clear

//Comentar no codigo final
//local lng pt

/****************** Pooled/Robustez ******************/
logit int_ris i.lingua##(i.com_pre c.perc_soc i.teayywhy i.renda i.ensino i.sexo c.idade_anos) i.continent i.colonized idh le ic delta_gdp delta_pop i.ano, robust
eststo int_risco_robust

est restore int_risco_robust
logit perc_opo i.lingua##(i.com_pre i.int_ris c.perc_soc i.teayywhy i.renda i.ensino i.sexo c.idade_anos) i.continent i.colonized idh le ic delta_gdp delta_pop i.ano if e(sample), robust
eststo oport_robust

est restore int_risco_robust
logit com_pre i.lingua##(c.perc_soc i.teayywhy i.renda i.ensino i.sexo c.idade_anos) i.continent i.colonized idh le ic delta_gdp delta_pop i.ano if e(sample), robust
eststo conhec_prev_robust

est restore int_risco_robust
reg perc_soc i.lingua##(i.teayywhy i.renda i.ensino i.sexo c.idade_anos) i.continent i.colonized idh le ic delta_gdp delta_pop i.ano if e(sample), robust
eststo perc_socied_robust

est restore int_risco_robust
ologit teayywhy i.lingua##(i.renda i.ensino i.sexo c.idade_anos) i.continent i.colonized idh le ic delta_gdp delta_pop i.ano if e(sample), robust
eststo motiv_robust

local params obslast pr2(%9.4f) t(%9.4f) b(%16.4f) sfmt(%9.4f %9.0f) nolines nogaps label compress nobaselevels aic bic ar2 star(* 0.1 ** 0.05 *** 0.01) refcat(1.teayywhy "Motiv. p/ Empreender:" 1.renda "Renda:" 1.ensino "Escolaridade:", nolabel) order(*.lingua *.com_pre *.int_ris perc_soc *.perc_soc *.teayywhy *.sexo idade_anos *.idade_anos *.renda *.ensino idh le ic delta_gdp delta_pop) indicate("Efeito fixo de ano = *.ano" "Efeito fixo de colonizado = *.colonized" "Efeito fixo Am. Sul = *.continent" /*"Efeito fixo de país = *.country"*/, labels(Sim Não))

esttab oport_robust int_risco_robust conhec_prev_robust perc_socied_robust motiv_robust, `params'

esttab oport_robust int_risco_robust conhec_prev_robust perc_socied_robust motiv_robust using out3_robustez.rtf, `params' replace
if "`lng'" == "pt" {
	unicode convertfile out3_robustez.rtf out3_robustez-pt.rtf, dstencoding(ISO-8859-1) replace
}


//Comentar no codigo final
//local lng pt

/****************** Pooled/Original ******************/
est restore int_risco_robust
logit int_ris i.lingua##(i.com_pre c.perc_soc i.teayywhy i.renda i.ensino i.sexo c.idade_anos) i.continent i.colonized i.ano if e(sample), robust
eststo int_risco

est restore int_risco_robust
logit perc_opo i.lingua##(i.com_pre i.int_ris c.perc_soc i.teayywhy i.renda i.ensino i.sexo c.idade_anos) i.continent i.colonized i.ano if e(sample), robust
eststo oport

est restore int_risco_robust
logit com_pre i.lingua##(c.perc_soc i.teayywhy i.renda i.ensino i.sexo c.idade_anos) i.continent i.colonized i.ano if e(sample), robust
eststo conhec_prev

est restore int_risco_robust
reg perc_soc i.lingua##(i.teayywhy i.renda i.ensino i.sexo c.idade_anos) i.continent i.colonized i.ano if e(sample), robust
eststo perc_socied

est restore int_risco_robust
ologit teayywhy i.lingua##(i.renda i.ensino i.sexo c.idade_anos) i.continent i.colonized i.ano if e(sample), robust
eststo motiv

local params obslast pr2(%9.4f) t(%9.4f) b(%16.4f) sfmt(%9.4f %9.0f) nolines nogaps label compress nobaselevels aic bic ar2 star(* 0.1 ** 0.05 *** 0.01) refcat(1.teayywhy "Motiv. p/ Empreender:" 1.renda "Renda:" 1.ensino "Escolaridade:", nolabel) order(*.lingua *.com_pre *.int_ris perc_soc *.perc_soc *.teayywhy *.sexo idade_anos *.idade_anos *.renda *.ensino) indicate("Efeito fixo de ano = *.ano" "Efeito fixo de colonizado = *.colonized" "Efeito fixo Am. Sul = *.continent" /*"Efeito fixo de país = *.country"*/, labels(Sim Não))

esttab oport int_risco conhec_prev perc_socied motiv, `params'

esttab oport int_risco conhec_prev perc_socied motiv using out3.rtf, `params' replace
if "`lng'" == "pt" {
	unicode convertfile out3.rtf out3-pt.rtf, dstencoding(ISO-8859-1) replace
}


//Comentar no codigo final
//local lng pt

/****************** EF País/LPM ******************/
est restore int_risco_robust
reg int_ris i.lingua##(i.com_pre c.perc_soc i.teayywhy i.renda i.ensino i.sexo c.idade_anos) idh le ic delta_gdp delta_pop i.ano i.country if e(sample), robust
eststo int_risco_fe

est restore int_risco_robust
reg perc_opo i.lingua##(i.com_pre i.int_ris c.perc_soc i.teayywhy i.renda i.ensino i.sexo c.idade_anos) idh le ic delta_gdp delta_pop i.ano i.country if e(sample), robust
eststo oport_fe

est restore int_risco_robust
reg com_pre i.lingua##(c.perc_soc i.teayywhy i.renda i.ensino i.sexo c.idade_anos) idh le ic delta_gdp delta_pop i.ano i.country if e(sample), robust
eststo conhec_prev_fe

est restore int_risco_robust
reg perc_soc i.lingua##(i.teayywhy i.renda i.ensino i.sexo c.idade_anos) idh le ic delta_gdp delta_pop i.ano i.country if e(sample), robust
eststo perc_socied_fe

est restore int_risco_robust
reg teayywhy i.lingua##(i.renda i.ensino i.sexo c.idade_anos) idh le ic delta_gdp delta_pop i.ano i.country if e(sample), robust
eststo motiv_fe

local params obslast pr2(%9.4f) t(%9.4f) b(%16.4f) sfmt(%9.4f %9.0f) nolines nogaps label compress nobaselevels aic bic ar2 star(* 0.1 ** 0.05 *** 0.01) refcat(1.teayywhy "Motiv. p/ Empreender:" 1.renda "Renda:" 1.ensino "Escolaridade:", nolabel) order(*.com_pre *.int_ris perc_soc *.perc_soc *.teayywhy *.sexo idade_anos *.idade_anos *.renda *.ensino idh le ic delta_gdp delta_pop) indicate("Efeito fixo de ano = *.ano" /*"Efeito fixo de colonizado = *.colonized" "Efeito fixo Am. Sul = *.continent"*/ "Efeito fixo de país = *.country", labels(Sim Não))

esttab oport_fe int_risco_fe conhec_prev_fe perc_socied_fe motiv_fe, `params'

esttab oport_fe int_risco_fe conhec_prev_fe perc_socied_fe motiv_fe using out3_fe.rtf, `params' replace
if "`lng'" == "pt" {
	unicode convertfile out3_fe.rtf out3_fe-pt.rtf, dstencoding(ISO-8859-1) replace
}


//Comentar no codigo final
//local lng pt

/****************** POLS/LPM ******************/
est restore int_risco_robust
reg int_ris i.lingua##(i.com_pre c.perc_soc i.teayywhy i.renda i.ensino i.sexo c.idade_anos) idh le ic delta_gdp delta_pop i.continent i.colonized i.ano if e(sample), robust
eststo int_risco_lpm

est restore int_risco_robust
reg perc_opo i.lingua##(i.com_pre i.int_ris c.perc_soc i.teayywhy i.renda i.ensino i.sexo c.idade_anos) idh le ic delta_gdp delta_pop i.continent i.colonized i.ano if e(sample), robust
eststo oport_lpm

est restore int_risco_robust
reg com_pre i.lingua##(c.perc_soc i.teayywhy i.renda i.ensino i.sexo c.idade_anos) idh le ic delta_gdp delta_pop i.continent i.colonized i.ano if e(sample), robust
eststo conhec_prev_lpm

est restore int_risco_robust
reg perc_soc i.lingua##(i.teayywhy i.renda i.ensino i.sexo c.idade_anos) idh le ic delta_gdp delta_pop i.continent i.colonized i.ano if e(sample), robust
eststo perc_socied_lpm

est restore int_risco_robust
reg teayywhy i.lingua##(i.renda i.ensino i.sexo c.idade_anos) i.continent i.colonized idh le ic delta_gdp delta_pop i.ano if e(sample), robust
eststo motiv_lpm

local params obslast pr2(%9.4f) t(%9.4f) b(%16.4f) sfmt(%9.4f %9.0f) nolines nogaps label compress nobaselevels aic bic ar2 star(* 0.1 ** 0.05 *** 0.01) refcat(1.teayywhy "Motiv. p/ Empreender:" 1.renda "Renda:" 1.ensino "Escolaridade:", nolabel) order(*.lingua *.com_pre *.int_ris perc_soc *.perc_soc *.teayywhy *.sexo idade_anos *.idade_anos *.renda *.ensino idh le ic delta_gdp delta_pop) indicate("Efeito fixo de ano = *.ano" "Efeito fixo de colonizado = *.colonized" "Efeito fixo Am. Sul = *.continent" /*"Efeito fixo de país = *.country"*/, labels(Sim Não))

esttab oport_lpm int_risco_lpm conhec_prev_lpm perc_socied_lpm motiv_lpm, `params'

esttab oport_lpm int_risco_lpm conhec_prev_lpm perc_socied_lpm motiv_lpm using out3_lpm.rtf, `params' replace
if "`lng'" == "pt" {
	unicode convertfile out3_lpm.rtf out3_lpm-pt.rtf, dstencoding(ISO-8859-1) replace
}

est restore int_risco
local clist "Paises: "
levelsof country if e(sample), local(paises)
levelsof ano if e(sample), local(anos)

local lbe: value label country
foreach l of local paises {
	local c`l' : label `lbe' `l'
	local clist "`clist' `c`l''; "
	//local filter_countries `filter_countries' | pais == `l'
}
di "`clist'"

estpost summarize lingua perc_opo com_pre int_ris perc_soc teayywhy sexo idade_anos renda ensino idh le db ic delta_gdp delta_pop if e(sample), detail
esttab . using descriptives.rtf, replace cells ("count mean(fmt(4)) sd(fmt(4)) min p25 p50 p75 max") noobs page label 

est restore int_risco
estpost tabulate country if e(sample)
esttab . using descriptives.rtf, append cells("b pct(fmt(2)) cumpct(fmt(2))") noobs label

est restore int_risco
estpost tabulate idade_anos if e(sample)
esttab . using descriptives.rtf, append cells("b pct(fmt(2)) cumpct(fmt(2))") noobs label

est restore int_risco
estpost correlate lingua perc_opo com_pre int_ris perc_soc teayywhy sexo idade_anos renda ensino idh le ic delta_gdp delta_pop if e(sample), matrix listwise 
esttab . using correlation.rtf, replace unstack not noobs compress star(* 0.1 ** 0.05 *** 0.01) label b(%16.4f)

est restore int_risco
estpost sum perc_opo com_pre int_ris perc_soc teayywhy sexo idade_anos renda ensino idh le ic delta_gdp delta_pop if e(sample) & lingua==1
matrix meanf1=e(mean)
matrix list meanf1

est restore int_risco
estpost sum perc_opo com_pre int_ris perc_soc teayywhy sexo idade_anos renda ensino idh le ic delta_gdp delta_pop if e(sample) & lingua==0
matrix meanf0=e(mean)
matrix list meanf0

est restore int_risco
estpost ttest perc_opo com_pre int_ris perc_soc teayywhy sexo idade_anos renda ensino idh le ic delta_gdp delta_pop if e(sample), by(lingua) unequal
estadd matrix meanf1
estadd matrix meanf0

local params noobs cells("meanf0(fmt(4)) meanf1(fmt(4)) b(star fmt(4)) se(fmt(4)) N_1(fmt(0)) N_2(fmt(0))") star(* 0.1 ** .05 *** 0.01) label
if "`lng'" == "pt" {
	local params `params' collabels("Média(Cultura=ES)" "Média(Cultura=PT)" "Difer." "Erro Pad." "Obs. ES" "Obs. PT")
}
else {
	local  params`params' collabels("Mean(Culture=ES)" "Mean(Culture=PT)" "Diff." "Std. Error" "Obs. ES" "Obs. PT")
} 
esttab . using ttest.rtf, `params' replace


if "`lng'" == "pt" {
	unicode convertfile descriptives.rtf descritivas.rtf, dstencoding(ISO-8859-1) replace
	unicode convertfile correlation.rtf correlacao.rtf, dstencoding(ISO-8859-1) replace
	unicode convertfile ttest.rtf testet.rtf, dstencoding(ISO-8859-1) replace
}

log close